#include <stdio.h>
#include <stdlib.h>
#include <libxml/tree.h>
#include "draw.h"
#include "defs.h"
#include "ai.h"
#include "physics.h"
#include "game.h"
#include "menu.h"
//#include <SDL/SDL_net.h>
#include "math.h"
#include "NintendoDS.h"

#ifdef __PSP__
#include "PSP.h"
SceCtrlData pad;
extern "C" int SDL_main(int argc, char *argv[]);
#endif

SDL_Surface *back1, *back2;
CSpriteBase balll,paddlee;

#ifndef __NDS__
	#define POWERUPS_PATH "src/data/powerups"
	#define DOUBLE_SPEED_PATH "src/data/powerups/DoubleSpeed"
	#define SPLIT_PATH "src/data/powerups/Split"
	#define HALF_SPEED_PATH "src/data/powerups/HalfSpeed"
	#define INC_SPEED_PATH "src/data/powerups/IncSpeed"
	#define DEC_SPEED_PATH "src/data/powerups/DecSpeed"
	#define BALL_PATH "src/data/ball"
	#define PADDLE_PATH "src/data/paddle"
	#define SOUND_PUK_PATH "src/data/sound/puk.wav"
	#define SOUND_PLUCK_PATH "src/data/sound/pluck.wav"
	#define FONT_PATH "src/data/font1"
	#define CONF_XML_PATH "src/data/conf.xml"
#else
	#define POWERUPS_PATH "/dades2Pong/powerups"
	#define DOUBLE_SPEED_PATH "/dades2Pong/powerups/DoubleSpeed"
	#define SPLIT_PATH "/dades2Pong/powerups/Split"
	#define HALF_SPEED_PATH "/dades2Pong/powerups/HalfSpeed"
	#define INC_SPEED_PATH "/dades2Pong/powerups/IncSpeed"
	#define DEC_SPEED_PATH "/dades2Pong/powerups/DecSpeed"
	#define BALL_PATH "/dades2Pong/ball"
	#define PADDLE_PATH "/dades2Pong/paddle"
	#define SOUND_PUK_PATH "/dades2Pong/sound/puk.wav"
	#define SOUND_PLUCK_PATH "/dades2Pong/sound/pluck.wav"
	#define FONT_PATH "/dades2Pong/font2"     // in NDS we use font2, that is a smaller font
	#define CONF_XML_PATH "/dades2Pong/conf.xml"
#endif


int InitImages()
{
//  back1 = ImageLoad("data/bg1.bmp");
// back2 = ImageLoad("data/bg2.bmp");

	return 0;
}

void FreeStuff(SDL_Surface * screen, Ball *balls, Paddle *paddles,  CSpriteBase * powerup_sprite, Powerup *powerups,defines def) {
	int i;
	for (i = 0; i < def.GetMaxPowerups(); i++)
		powerup_sprite[i].destroy();
	for (i = 0; i < def.GetMaxBalls(); i++)	
		balls[i].destroy();
	for (i = 0; i < def.GetMaxPaddles(); i++)
		paddles[i].destroy();
	SDL_FreeSurface(screen);
}

void FirstInitPowerups(Powerup *powerups, defines def, CSpriteBase * powerup_sprite) {
	/*for (int i = 0; i < def.GetMaxPowerups(); i++) {
		powerup_sprite[i].init("src/data/powerups");
	}*/
	powerup_sprite[0].init((char *)POWERUPS_PATH);
	powerup_sprite[1].init((char *)DOUBLE_SPEED_PATH);
	powerup_sprite[2].init((char *)SPLIT_PATH);
	powerup_sprite[3].init((char *)HALF_SPEED_PATH);
	powerup_sprite[4].init((char *)INC_SPEED_PATH);
	powerup_sprite[5].init((char *)DEC_SPEED_PATH);
	powerup_sprite[6].init((char *)POWERUPS_PATH);
}

void FirstInitBalls(Ball *balls, SDL_Surface * screen, defines def) {
	balll.init((char *)BALL_PATH);
	for (int i = 0; i < MAX; i++) {
		balls[i].init(&balll,screen);
		balls[i].SetSize(def.GetBallSize());
	}
}

void FirstInitPaddles(Paddle *paddles, SDL_Surface * screen, defines def) {
	paddlee.init((char *)PADDLE_PATH);
	for (int j = 0; j < def.GetMaxPaddles(); j++) {
		paddles[j].init(&paddlee,screen);
		paddles[j].SetSize(SIZE_PADDLE_X,SIZE_PADDLE_Y);
	}
	paddles[0].SetType(AI);
	paddles[1].SetType(Human);
	paddles[1].SetControl(Keyb);
}


void FirstInit(Ball *balls, Paddle *paddles, Powerup *powerups, SDL_Surface * screen, defines def, CSpriteBase * powerup_sprite) {
	InitImages();
	FirstInitBalls(balls,screen, def);
	FirstInitPaddles(paddles,screen, def);
	FirstInitPowerups(powerups,def,powerup_sprite);
}



#ifdef __PSP__
int SDL_main(int argc, char *argv[])
#else
int main(int argc, char *argv[])
#endif
{
	CSpriteBase powerup_sprite[MAX];
	Mix_Chunk *puk, *pluck;
	defines def;
	srand(time(NULL));
	if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
	{
		printf("Unable to init SDL: %s\n", SDL_GetError());
	exit(1);
	}

	#ifdef __NDS__
	consoleDemoInit();
	consoleDebugInit(DebugDevice_CONSOLE);
	bool iniok = fatInitDefault();
	if(!iniok) printf("ERROR al inicializar FAT\n");
	#endif

	atexit(SDL_Quit);

	#if !defined (__NDS__) &&  !defined (__PSP__)
	if(Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 2048) < 0)
	{
		printf("Warning: Couldn't set 44100 Hz 16-bit audio\n- Reason: %s\n", SDL_GetError());
	}
	puk = Mix_LoadWAV((char *)SOUND_PUK_PATH);
	pluck = Mix_LoadWAV((char *)SOUND_PLUCK_PATH);
	#endif

	Game game;
	game.SetLimit(NUMBER_BALLS);
	SDL_WM_SetIcon(SDL_LoadBMP("icon.bmp"),NULL);
	SDL_WM_SetCaption("2Pong [1.0a]","2Pong [1.0a]");
	SDL_Surface * screen;

	//For Linux
	screen = SDL_SetVideoMode(def.GetWindowWidth(),def.GetWindowHeight(), BITS_PER_PIXEL ,SDL_SWSURFACE);
	//For Windows
	// screen = SDL_SetVideoMode(def.GetWindowWidth(),def.GetWindowHeight(),32,SDL_HWSURFACE|SDL_DOUBLEBUF);

	if ( screen == NULL )
	{
		printf("Unable to set %dx%d video: %s\n", def.GetWindowWidth(),def.GetWindowHeight(),SDL_GetError());
		exit(1);
	}
	Ball balls[MAX];
	Paddle paddles[MAX];
	Powerup powerups[MAX];

	#if !defined (__NDS__) &&  !defined (__PSP__)
		SDL_ShowCursor(1);
	#else
		SDL_ShowCursor(0);
	#endif

	//InitFonts()
	SDLFont * font1;
	font1 = initFont((char *)FONT_PATH);
	game.SetFont(font1);
	game.SetScreen(screen);
	//In the future, dynamic limit.

	FirstInit(balls,paddles,powerups,game.GetScreen(), def,powerup_sprite);
	InitMenu((char *)CONF_XML_PATH,&game,balls,paddles,powerups,def,powerup_sprite,puk,pluck);

	freeFont(font1);
	#ifndef __PSP__
	Mix_FreeChunk(puk);
	Mix_FreeChunk(pluck);
	#endif
	FreeStuff(screen,balls,paddles, powerup_sprite,powerups, def);
	return(0);
}
